package com.example.nftapp.frg.find;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.nftapp.R;
import com.example.nftapp.act.WebActivity;
import com.example.nftapp.adapter.BannerAdapterHttp;
import com.example.nftapp.beans.BannerHttp;
import com.example.nftapp.http.HttpCallback;
import com.example.nftapp.http.HttpResponse;
import com.example.nftapp.http.HttpUtil;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class DiscoveryFragment extends Fragment {

    private final List<BannerHttp> bannerData = new ArrayList<>();
    private BannerAdapterHttp bannerAdapterHttp;
    //Handler+Runnable实现自动轮播
    private Handler handler;
    private ViewPager2 vpBanner;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_discovery, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TabLayout tabBanner = view.findViewById(R.id.tabBanner);
        vpBanner = view.findViewById(R.id.vpBanner);
        bannerAdapterHttp = new BannerAdapterHttp(bannerData, position -> {
            // 点击跳转至WebActivity，加载显示文章
            // ToastUtil.showToast(requireContext(), bannerData.get(position).getUrl());
            Intent intent = new Intent(requireContext(), WebActivity.class);
            intent.putExtra("url", bannerData.get(position).getUrl());
            startActivity(intent);
        });
        vpBanner.setAdapter(bannerAdapterHttp);
        new TabLayoutMediator(tabBanner, vpBanner, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                ImageView imageView = (ImageView) LayoutInflater.from(requireActivity()).inflate(R.layout.layout_custom_tab_banner, null);
                tab.setCustomView(imageView);
                if (position == 0) {
                    imageView.setImageResource(R.drawable.bg_dot_selected);
                }
            }
        }).attach();
        tabBanner.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                ImageView imageView = (ImageView) tab.getCustomView();
                assert imageView != null;
                imageView.setImageResource(R.drawable.bg_dot_selected);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ImageView imageView = (ImageView) tab.getCustomView();
                assert imageView != null;
                imageView.setImageResource(R.drawable.bg_dot_unselected);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        HttpUtil.doGet("https://www.wanandroid.com/banner/json", new HttpCallback() {
            @Override
            public void onHttpSucceed(HttpResponse httpResponse) {
                requireActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for (LinkedTreeMap map : (List<LinkedTreeMap>) httpResponse.getData()) {
                            bannerData.add(new BannerHttp((String) map.get("imagePath"), (String) map.get("url")));
                        }
                        bannerAdapterHttp.notifyDataSetChanged();
                        startAutoScroll();
                    }
                });
            }
        });
    }

    /**
     * 开启自动轮播
     */
    private void startAutoScroll() {
        handler = new Handler(Looper.getMainLooper());
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                vpBanner.setCurrentItem((vpBanner.getCurrentItem() + 1) % bannerData.size(), true);
                handler.postDelayed(this, 3000);
            }
        };
        handler.postDelayed(runnable, 3000);
    }

}